package me.ak.sf.level;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import me.ak.sf.SlothfallGameState;
import org.newdawn.slick.Graphics;

public abstract class Level {

    public SlothfallGameState parent;
    
    
    
    public Level() {
       
    }
    
    public void init(SlothfallGameState s) {
        parent = s;
    }
    
    public abstract int getId();
    
    public abstract void update();
    
    public abstract void renderBackground(Graphics g);
    public abstract void renderForeground(Graphics g);
    

    
    
    
    
    
    
    
    
    
    
    /*
    STATIC STUFF
    */
    public static HashMap<Integer, Level> levels = new HashMap<Integer, Level>();

    public static Level getLevel(int i) {
        if (!levels.containsKey(i)) {
            return null;
        }
        return levels.get(i);
    }

    public static void loadLevels() {
        ClassLoader cl = ClassLoader.getSystemClassLoader();
        File levelDir = new File(Level.class.getResource("/resources/levels").getPath());
        List<String> levelPaths = new ArrayList<String>();
        for (File f : levelDir.listFiles()) {
            String extension = "";

            int i = f.getName().lastIndexOf('.');
            if (i > 0) {
                extension = f.getName().substring(i + 1);
            }
            if (extension.equals("class")) {
                levelPaths.add(f.getName().substring(0, i));
            }
        }
        for (String lc: levelPaths) {
            try {
                Class level = cl.loadClass("resources.levels." + lc);
                if (level.getSuperclass().equals(Level.class)){
                    Level l = (Level) level.newInstance();
                    levels.put(l.getId(), l);
                    System.out.println("Added level " + l);
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
